package com.woniu.microcommunityserver.advert.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.microcommunityserver.advert.entity.AdvertSlideshowInfoEntity;
import com.woniu.microcommunityserver.advert.service.AdvertSlideshowInfoService;
import com.woniu.microcommunityserver.res.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.function.ServerResponse;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("advert")
public class AdvertSlideshowInfoController {
    @Resource
    private AdvertSlideshowInfoService advertSlideshowInfoService;

    @GetMapping("find")
    public ResponseMsg find(int pageNum){
        PageHelper.startPage(pageNum,10);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<AdvertSlideshowInfoEntity> advert = advertSlideshowInfoService.find();
        PageInfo<AdvertSlideshowInfoEntity> pageInfo = new PageInfo<AdvertSlideshowInfoEntity>(advert);
        return ResponseMsg.builder().code(200).msg("所有广告轮播图查询成功！").data(pageInfo).build();
    }

    @GetMapping("findByState")
    public ResponseMsg findByState(int pageNum,int slideshowState){
        PageHelper.startPage(pageNum,10);//这行是重点，表示从pageNum页开始，每页pageSize条数据
        List<AdvertSlideshowInfoEntity> advert = advertSlideshowInfoService.findByState(slideshowState);
        PageInfo<AdvertSlideshowInfoEntity> pageInfo = new PageInfo<AdvertSlideshowInfoEntity>(advert);
        return ResponseMsg.builder().code(200).msg("指定广告轮播图查询成功！").data(pageInfo).build();
    }

    @GetMapping("update")
    public ResponseMsg updateStateById(Integer slideshowState, Long id){
        boolean flag=advertSlideshowInfoService.updateStateById(slideshowState, id);
        if (flag){
            return ResponseMsg.builder().code(200).msg("修改成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("修改失败！").build();
        }
    }

    @PostMapping(value = "insert")
    public ResponseMsg insertAdvert(@RequestBody AdvertSlideshowInfoEntity advertSlideshowInfoEntity){
        System.out.println(advertSlideshowInfoEntity);
        advertSlideshowInfoEntity.setSlideshowState(1);
        boolean flag = advertSlideshowInfoService.insertAdvert(advertSlideshowInfoEntity);
        if (flag){
            return ResponseMsg.builder().code(200).msg("新增成功！").build();
        }else {
            return ResponseMsg.builder().code(403).msg("新增失败！").build();
        }
    }
}
